package com.wuyou.mid;

/**
 * 769. 最多能完成排序的块
 */
public class MaxChunksToSorted {

    public static void main(String[] args) {

        MaxChunksToSorted maxChunksToSorted = new MaxChunksToSorted();
        int[] arr = {1, 2, 0, 3};
        System.out.println(maxChunksToSorted.solution_1(arr));
    }

    public int solution_1(int[] arr) {
        int res = 0, flag = 0, x = 0;
        for (int i = 0; i < arr.length; i++) {
            flag = Math.max(flag, arr[i]);
            if (arr[i] == x) {
                if (flag == i) {
                    res += 1;
                    x = i + 1;
                }
            }else {
                if (flag == i) {
                    res += 1;
                }
            }
        }
        return res;
    }
}
